package com.example.administrator_kunker.mjj_pro;

import android.app.Application;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.example.administrator_kunker.mjj_pro.util.AndroidClient;
import com.vilyever.socketclient.SocketClient;
import com.vilyever.socketclient.helper.SocketClientDelegate;
import com.vilyever.socketclient.helper.SocketClientReceivingDelegate;
import com.vilyever.socketclient.helper.SocketResponsePacket;
import com.vilyever.socketclient.util.CharsetUtil;
import com.vilyever.socketclient.util.IPUtil;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,SocketClientDelegate ,SocketClientReceivingDelegate {

    @BindView(R.id.send_area)
    EditText send_area;
    @BindView(R.id.receive_area)
    EditText receive_area;
    @BindView(R.id.bt_send)
    EditText bt_send;
    @BindView(R.id.bt_set)
    EditText bt_set;
    private AndroidClient aclient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        init();
    }

    @Override
    protected void onPause() {
        super.onPause();
        aclient.destory();
    }

    @Override
    protected void onResume() {
        super.onResume();
        init();
    }

    private void init(){
        if(aclient == null){
            aclient = ((ApplicationLocal)MainActivity.this.getApplication()).getAclient();
        }
        if(aclient.state()){
            ((ApplicationLocal)MainActivity.this.getApplication()).updateConfig();
            aclient.connect();
        }
        aclient.addListener(this,this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_send:
                aclient.sendCmd(send_area.getText().toString());
                break;
            case R.id.bt_set:
                startActivity(new Intent(MainActivity.this,SetActivity.class));
                break;
        }
    }
    //连接成功后
    @Override
    public void onConnected(SocketClient client) {

    }

    @Override
    public void onDisconnected(SocketClient client) {

    }

    @Override
    public void onResponse(SocketClient client, @NonNull SocketResponsePacket responsePacket) {

    }

    @Override
    public void onReceivePacketBegin(SocketClient client, SocketResponsePacket packet) {

    }
    //接收完成
    @Override
    public void onReceivePacketEnd(SocketClient client, SocketResponsePacket packet) {
        String msg = packet.getMessage();
        receive_area.append(msg + "\r\n");
    }

    @Override
    public void onReceivePacketCancel(SocketClient client, SocketResponsePacket packet) {

    }

    @Override
    public void onReceivingPacketInProgress(SocketClient client, SocketResponsePacket packet, float progress, int receivedLength) {

    }
}
